﻿package frEngine.effectEditTool.manager
{
    import __AS3__.vec.*;
    import baseEngine.basic.*;
    import baseEngine.core.*;
    import baseEngine.modifiers.*;
    import baseEngine.system.*;
    import flash.geom.*;
    import flash.utils.*;
    import frEngine.*;
    import frEngine.animateControler.*;
    import frEngine.animateControler.keyframe.*;
    import frEngine.animateControler.visible.*;
    import frEngine.core.mesh.*;
    import frEngine.effectEditTool.obj3d.*;
    import frEngine.effectEditTool.parser.*;
    import frEngine.effectEditTool.temple.*;
    import frEngine.event.*;
    import frEngine.primitives.lightingline.bullet.*;
    import frEngine.shader.*;
    import frEngine.shader.filters.fragmentFilters.*;

    public class Obj3dContainer extends Pivot3D
    {
        public var impRemove:Boolean = true;
        public var id:int;
        private var _useFightedPoint:Boolean = false;
        private var _idToObject3dMap:Dictionary;
        private var _object3dToIdMap:Dictionary;
        private var _addToRenderList:Boolean;
        private var _temple:ITemple;
        private var _templeName:String = "-1";
        public var templeNeedUpdate:Boolean = false;
        private var _isPlaying:Boolean = true;
        public var maxKeyFrame:uint = 4.29497e+009;
        private var _renderList:RenderList;
        public var shakeObj:Object;
        public var game3DControler:Game3DControler;
        private var _layers:Array;
        protected var _effectUrl:String;
        public var clearEnabled:Boolean = true;
        protected var __timeControler:TimeControler;
        private var _colorScreenValue:Boolean = false;
        private var _hasStartShake:Boolean = false;
        private static const _containerMap:Dictionary = new Dictionary(false);
        public static var sceneObjectsList:Dictionary = new Dictionary(false);
        public static const shakeUid:String = "smoothEditor";

        public function Obj3dContainer(param1:String, param2:int, param3:Boolean = false, param4:TimeControler = null)
        {
            this._idToObject3dMap = new Dictionary(false);
            this._object3dToIdMap = new Dictionary(false);
            this._layers = new Array();
            super(param1);
            this._addToRenderList = param3;
            this.id = param2;
            this.__timeControler = TimeControler.createTimerInstance(null, null, this.id);
            this.timerContorler = param4 == null ? (this.__timeControler) : (param4);
            return;
        }// end function

        public function get effectUrl() : String
        {
            return this._effectUrl;
        }// end function

        public function set effectUrl(param1:String) : void
        {
            if (this.isInPool || this.isDisposed)
            {
                trace("error in EffectPlayer.as");
            }
            this._effectUrl = param1;
            return;
        }// end function

        public function get renderList() : RenderList
        {
            return this._renderList;
        }// end function

        public function set renderList(param1:RenderList) : void
        {
            var _loc_2:Mesh3D = null;
            if (this._renderList == param1)
            {
                return;
            }
            this._renderList = param1;
            for each (_loc_2 in this._idToObject3dMap)
            {
                
                _loc_2.renderList = this._renderList;
            }
            return;
        }// end function

        public function get isPlaying() : Boolean
        {
            return this._isPlaying;
        }// end function

        public function setData(param1:Object, param2:Boolean, param3:Boolean, param4:RenderList) : void
        {
            var _loc_8:Object = null;
            var _loc_9:Object = null;
            this.disposeAllChildren();
            this.shakeObj = null;
            if (!param1)
            {
                return;
            }
            this.maxKeyFrame = param1.maxKeyFrame;
            if (param3)
            {
                this.setTempleByName(param1.templeName);
            }
            var _loc_5:* = param1.layers;
            this._layers = _loc_5;
            var _loc_6:* = _loc_5.length;
            var _loc_7:int = 0;
            while (_loc_7 < _loc_6)
            {
                
                _loc_8 = _loc_5[_loc_7];
                ParserLayerObject.instance.parserParentLayer(this, _loc_8.parentLayer, param2, param4);
                _loc_7++;
            }
            _loc_7 = 0;
            while (_loc_7 < _loc_6)
            {
                
                _loc_8 = _loc_5[_loc_7];
                ParserLayerObject.instance.parserChildrenLayer(this, _loc_8.parentLayer.uid, _loc_8.childLayers);
                _loc_7++;
            }
            _loc_7 = 0;
            while (_loc_7 < _loc_6)
            {
                
                _loc_9 = this._layers[_loc_7].parentLayer;
                ParserLayerObject.instance.parserLayerHang(this, _loc_9.uid, _loc_9.hangObjId, _loc_9.hangBoneName, _loc_9.hangToTemple);
                _loc_7++;
            }
            if (this._temple)
            {
                this._temple.parsersParams(param1.templeParams);
            }
            if (param1.gameAttr)
            {
                this.shakeObj = param1.gameAttr.shake;
            }
            if (this._colorScreenValue == true)
            {
                this.coloredScreen = true;
            }
            return;
        }// end function

        public function parserLayerHang(param1:Boolean) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:Pivot3D = null;
            if (!this._layers)
            {
                return;
            }
            var _loc_2:* = this._layers.length;
            _loc_3 = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = this._layers[_loc_3].parentLayer;
                _loc_5 = this.getObject3dByID(_loc_4.uid);
                if (_loc_5 is ParticleMesh || _loc_5 is GeometryLighting || _loc_4.hangToTemple == param1)
                {
                    ParserLayerObject.instance.parserLayerHang(this, _loc_4.uid, _loc_4.hangObjId, _loc_4.hangBoneName, _loc_4.hangToTemple);
                }
                _loc_3++;
            }
            return;
        }// end function

        public function setTempleByName(param1:String) : void
        {
            var _loc_2:ITemple = null;
            if (this._templeName == param1 && this._temple)
            {
                return;
            }
            this._templeName = param1;
            switch(param1)
            {
                case "test2t":
                {
                    _loc_2 = new TempleRole(this);
                    break;
                }
                case ETempleType.Fighted:
                {
                    _loc_2 = new TempleRole(this);
                    break;
                }
                case ETempleType.Role:
                {
                    _loc_2 = new TempleRole(this);
                    break;
                }
                case ETempleType.Fight:
                {
                    _loc_2 = new TempleFight(this);
                    break;
                }
                case ETempleType.Lighting:
                {
                    _loc_2 = new TempleLighting(this);
                    break;
                }
                default:
                {
                    _loc_2 = new EmptyTemple();
                    break;
                    break;
                }
            }
            this._temple = _loc_2;
            return;
        }// end function

        public function get templeName() : String
        {
            return this._templeName;
        }// end function

        public function get temple() : ITemple
        {
            if (!this._temple)
            {
                this._temple = new EmptyTemple();
            }
            return this._temple;
        }// end function

        public function registerObject(param1:String, param2:String, param3:Mesh3D) : void
        {
            if (this._idToObject3dMap[param1] != null)
            {
                throw new Error(param1 + "已注册！");
            }
            this._idToObject3dMap[param1] = param3;
            this._object3dToIdMap[param3] = param1;
            _containerMap[param3] = this;
            param3.name = param2;
            if (this._addToRenderList)
            {
                sceneObjectsList[param3] = param3;
            }
            param3.timerContorler = this.timerContorler;
            return;
        }// end function

        public function resetTimerContorler(param1:TimeControler) : void
        {
            var _loc_2:Pivot3D = null;
            if (this.timerContorler == param1)
            {
                return;
            }
            for each (_loc_2 in this._idToObject3dMap)
            {
                
                _loc_2.timerContorler = param1;
            }
            this.timerContorler = param1;
            return;
        }// end function

        public function unRgisterObjectByID(param1:String) : void
        {
            var _loc_2:* = this._idToObject3dMap[param1];
            delete this._idToObject3dMap[param1];
            delete this._object3dToIdMap[_loc_2];
            delete _containerMap[_loc_2];
            delete sceneObjectsList[_loc_2];
            return;
        }// end function

        public function unRgisterObjectByObj3d(param1:Mesh3D) : void
        {
            var _loc_2:* = this._object3dToIdMap[param1];
            delete this._idToObject3dMap[_loc_2];
            delete this._object3dToIdMap[param1];
            delete sceneObjectsList[param1];
            return;
        }// end function

        public function getObject3dByID(param1:String) : Pivot3D
        {
            if (param1 == "root")
            {
                return this;
            }
            if (param1 == shakeUid)
            {
                return Global3D.scene.gameControler;
            }
            return this._idToObject3dMap[param1];
        }// end function

        public function getIdByObject3d(param1:Pivot3D) : String
        {
            if (!param1)
            {
                return "null";
            }
            return this._object3dToIdMap[param1];
        }// end function

        public function changeChildrenHang(param1:Pivot3D, param2:Pivot3D) : void
        {
            var _loc_4:Pivot3D = null;
            var _loc_5:String = null;
            var _loc_6:Md5SkinAnimateControler = null;
            var _loc_3:* = param1.children;
            if (!_loc_3)
            {
                return;
            }
            while (_loc_3.length > 0)
            {
                
                _loc_4 = _loc_3[0];
                if (_loc_4.curHangControler)
                {
                    _loc_5 = _loc_4.curHangControler.getMeshHangBoneName(_loc_4);
                    _loc_4.curHangControler.removeHang(_loc_4);
                    _loc_6 = param2.getAnimateControlerInstance(AnimateControlerType.Md5SkinAnimateControler) as Md5SkinAnimateControler;
                    if (_loc_6)
                    {
                        _loc_6.attachObjectToBone(_loc_5, _loc_4);
                    }
                    else
                    {
                        _loc_4.parent = param2;
                    }
                    continue;
                }
                _loc_4.parent = param2;
            }
            return;
        }// end function

        public function set coloredScreen(param1:Boolean) : void
        {
            var _loc_2:Mesh3D = null;
            var _loc_3:Mesh3D = null;
            this._colorScreenValue = param1;
            if (this._layers && this._layers.length > 0)
            {
                for each (_loc_2 in this._idToObject3dMap)
                {
                    
                    _loc_3 = Mesh3D(_loc_2);
                    _loc_3.material.materialParams.coloredScreen = this._colorScreenValue;
                }
            }
            return;
        }// end function

        public function get useFightedPoint() : Boolean
        {
            return this._useFightedPoint;
        }// end function

        public function set useFightedPoint(param1:Boolean) : void
        {
            if (param1 == this._useFightedPoint)
            {
                return;
            }
            this._useFightedPoint = param1;
            this.independentRot = param1;
            if (param1)
            {
                if (this._layers.length > 0)
                {
                    this.parserLayerHang(true);
                }
            }
            return;
        }// end function

        public function disposeChild(param1:Pivot3D) : void
        {
            var _loc_6:Mesh3D = null;
            var _loc_2:* = Global3D.scene;
            var _loc_3:* = new Array();
            this.getMeshesInObject(_loc_3, param1);
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_3[_loc_5];
                _loc_2.removeSelectObject(_loc_6, false);
                if (this._object3dToIdMap[_loc_6] != null)
                {
                    _loc_6.enabledPool = true;
                    _loc_6.isParticleElement = false;
                    _loc_6.dispose();
                    this.unRgisterObjectByObj3d(_loc_6);
                }
                else
                {
                    if (!_loc_6.isDisposed && !_loc_6.isInPool)
                    {
                        _loc_6.parent = _loc_2;
                    }
                    if (_loc_6.curHangControler)
                    {
                        _loc_6.curHangControler.removeHang(_loc_6);
                    }
                }
                _loc_5++;
            }
            return;
        }// end function

        public function getMeshesInObject(param1:Array, param2:Pivot3D) : void
        {
            var _loc_4:int = 0;
            if (param2.children == null)
            {
                return;
            }
            var _loc_3:* = param2.children.length;
            if (_loc_3 > 0)
            {
                _loc_4 = 0;
                while (_loc_4 < _loc_3)
                {
                    
                    this.getMeshesInObject(param1, param2.children[_loc_4]);
                    _loc_4++;
                }
            }
            if (param2 is Mesh3D)
            {
                param1.push(param2);
            }
            return;
        }// end function

        public function disposeAllChildren() : void
        {
            var _loc_4:Pivot3D = null;
            var _loc_1:* = this.getAllObject3d();
            var _loc_2:* = _loc_1.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = _loc_1[_loc_3];
                _loc_4.enabledPool = true;
                _loc_4.isParticleElement = false;
                this.disposeChild(_loc_4);
                _loc_3++;
            }
            this.__timeControler.gotoFrame(0, 0);
            return;
        }// end function

        public function getAllObject3d() : Array
        {
            var _loc_2:Pivot3D = null;
            var _loc_1:* = new Array();
            for each (_loc_2 in this._idToObject3dMap)
            {
                
                _loc_1.push(_loc_2);
            }
            return _loc_1;
        }// end function

        public function addChildByArray(param1:Vector.<Pivot3D>) : void
        {
            var _loc_5:Pivot3D = null;
            var _loc_2:* = param1.concat();
            var _loc_3:* = _loc_2.length;
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3)
            {
                
                _loc_5 = _loc_2[_loc_4];
                if (_loc_5.curHangControler)
                {
                    _loc_5.curHangControler.removeHang(_loc_5);
                }
                _loc_5.curHangControler = null;
                this.addChild(_loc_5);
                _loc_4++;
            }
            return;
        }// end function

        public function switchObjects(param1:Mesh3D, param2:Mesh3D, param3:Boolean) : void
        {
            var _loc_5:FragmentFilter = null;
            var _loc_6:Dictionary = null;
            var _loc_7:VisibleControler = null;
            var _loc_8:Pivot3D = null;
            var _loc_9:Md5SkinAnimateControler = null;
            var _loc_10:String = null;
            var _loc_13:String = null;
            var _loc_14:Boolean = false;
            var _loc_4:* = this.getIdByObject3d(param1);
            var _loc_11:* = new Matrix3D();
            if (param3)
            {
                param2.material.materialParams.cloneFrom(param1.material.materialParams);
            }
            var _loc_12:* = param1.getMaterialByName(null) as ShaderBase;
            if (_loc_12.materialParams.uvMaskFilter.maskTexture3D)
            {
                _loc_13 = _loc_12.materialParams.uvMaskFilter.maskTexture3D.request;
                _loc_14 = _loc_12.materialParams.uvMaskFilter.openSolubility;
            }
            _loc_5 = _loc_12.materialParams.fragmentFilter;
            _loc_6 = param1.animateControlerList;
            _loc_7 = param1.visibleControler;
            _loc_8 = param1.parent;
            _loc_9 = param1.curHangControler;
            param2.independentRot = param1.independentRot;
            param2.independentScale = param1.independentScale;
            if (_loc_9)
            {
                _loc_10 = _loc_9.getMeshHangBoneName(param1);
            }
            _loc_11.copyFrom(param1.transform);
            if (param1.offsetTransform)
            {
                param2.offsetTransform = param1.offsetTransform.clone();
            }
            param2.setLayer(param1.layer);
            param1.clearAllControlers(false);
            _loc_12.materialParams.setFragmentFilter(null);
            this.changeChildrenHang(param1, param2);
            this.unRgisterObjectByObj3d(param1);
            param1.dispose(true);
            param2.copyToControlers(_loc_6, _loc_7);
            this.registerObject(_loc_4, param2.name, param2);
            if (_loc_8)
            {
                _loc_8.addChild(param2);
            }
            else
            {
                this.addChild(param2);
            }
            param2.setTransform(_loc_11, true);
            if (_loc_13)
            {
                param2.material.materialParams.setMaskTextureUrl(_loc_13);
                param2.material.materialParams.uvMaskFilter.openSolubility = _loc_14;
            }
            if (_loc_5)
            {
                param2.setMaterial(_loc_5, Texture3D.MIP_NONE, "framentFilter");
            }
            if (_loc_9)
            {
                _loc_9.attachObjectToBone(_loc_10, param2);
            }
            return;
        }// end function

        override public function update(param1:int = 0) : void
        {
            if (this._isPlaying)
            {
                super.update(param1);
            }
            if (this.templeNeedUpdate && this._temple)
            {
                this._temple.update();
            }
            if (this.animationMode == 1)
            {
                if (this.timerContorler.totalframe >= this.maxKeyFrame)
                {
                    this.playEndHander();
                }
            }
            else if (this.animationMode == 0)
            {
                if (this.timerContorler.curFrame == (this.maxKeyFrame - 1))
                {
                    this.playEndHander();
                }
            }
            this.checkShake();
            return;
        }// end function

        protected function checkShake() : void
        {
            if (this.game3DControler && this.shakeObj)
            {
                if (this.timerContorler.curFrame > this.shakeObj.startFrame)
                {
                    if (!this._hasStartShake)
                    {
                        this.game3DControler.checkShake(this.shakeObj, this.timerContorler.curFrame - this.shakeObj.startFrame);
                        this._hasStartShake = true;
                    }
                }
                else
                {
                    this._hasStartShake = false;
                }
            }
            return;
        }// end function

        public function onFightHitedHander(param1:int, param2:Boolean) : void
        {
            var _loc_6:Pivot3D = null;
            var _loc_3:* = this.getAllObject3d();
            var _loc_4:* = _loc_3.length;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_4)
            {
                
                _loc_6 = _loc_3[_loc_5];
                if (_loc_6 is ParticleMesh && ParticleMesh(_loc_6).canImpStop == false)
                {
                    ParticleMesh(_loc_6).particleControler.emmitFlag = param1;
                    _loc_6.isHide = false;
                }
                else
                {
                    _loc_6.isHide = param2;
                }
                _loc_5++;
            }
            return;
        }// end function

        protected function playEndHander() : void
        {
            FrEventDispatcher.instance.proxyDispatchEvent(this, Engine3dEventName.PLAYEND);
            return;
        }// end function

        public function play(param1:int = 0) : void
        {
            this.animationMode = param1;
            this._isPlaying = true;
            this.__timeControler.active(this.id);
            this.__timeControler.gotoFrame(0, 0);
            this.temple.checkAndPlay();
            return;
        }// end function

        public function stop() : void
        {
            this._isPlaying = false;
            this.__timeControler.unActive(this.id);
            return;
        }// end function

        public function reset() : void
        {
            this._isPlaying = true;
            animationMode = PlayMode.ANIMATION_STOP_MODE;
            return;
        }// end function

        override public function dispose(param1:Boolean = true) : void
        {
            this._layers = [];
            this.__timeControler.unActive(this.id);
            this.clearEnabled = true;
            if (this.temple)
            {
                this.temple.dispose(null);
                this._temple = null;
            }
            this.disposeAllChildren();
            this.maxKeyFrame = uint.MAX_VALUE;
            super.dispose(param1);
            this.resetTimerContorler(this.__timeControler);
            return;
        }// end function

        public function rehangToTemple() : void
        {
            if (this.isDisposed || this.isInPool)
            {
                return;
            }
            this.parserLayerHang(true);
            return;
        }// end function

        public function removeAllHangToTemple(param1:Pivot3D) : void
        {
            var _loc_4:Object = null;
            var _loc_5:Pivot3D = null;
            var _loc_2:* = this._layers.length;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2)
            {
                
                _loc_4 = this._layers[_loc_3].parentLayer;
                if (_loc_4.hangToTemple)
                {
                    _loc_5 = this.getObject3dByID(_loc_4.uid);
                    if (_loc_5)
                    {
                        if (_loc_5.curHangControler)
                        {
                            _loc_5.curHangControler.removeHang(_loc_5);
                            _loc_5.curHangControler = null;
                        }
                        _loc_5.parent = param1;
                    }
                }
                _loc_3++;
            }
            return;
        }// end function

        public function updateMaskable(param1:Boolean) : void
        {
            var _loc_2:Mesh3D = null;
            for each (_loc_2 in this._idToObject3dMap)
            {
                
                _loc_2.maskEnable = param1;
                if (_loc_2.layer < 3)
                {
                    _loc_2.setLayer(20);
                }
            }
            return;
        }// end function

        public function updateIndepentScale(param1:Boolean) : void
        {
            var _loc_2:Mesh3D = null;
            for each (_loc_2 in this._idToObject3dMap)
            {
                
                if (_loc_2 is EmptyDummy)
                {
                    _loc_2.independentScale = param1;
                    _loc_2.worldTransformChanged = true;
                }
            }
            return;
        }// end function

        public static function getContainerByObject3d(param1:Pivot3D) : Obj3dContainer
        {
            return _containerMap[param1];
        }// end function

    }
}
